package com.cn.happymall.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cn.happymall.bean.MmallUser;
import com.cn.happymall.mapper.MmallUserMapper;
import com.cn.happymall.util.MD5Util;

/**
 * @ClassName UserServiceImpl
 * @Description TODO(这里用一句话描述这个类的作用)
 * @author hasee
 * @Date 2017年12月25日 下午2:31:58
 * @version 1.0.0
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {

	@Autowired
	private MmallUserMapper mapper;

	@Override
	public MmallUser login(String username, String password) {

		String pwd = MD5Util.MD5EncodeUtf8(password);

		MmallUser user = mapper.findUser(username, pwd);
		return user;
	}

	@Override
	public int checkNameOrEmail(String str, String type) {

		if (type.equals("username")) {
			return mapper.findByName(str);
		} else if (type.equals("email")) {
			return mapper.findByEmail(str);
		}

		return 0;
	}

	@Override
	public int register(MmallUser user) {

		String password = user.getPassword();
		String md5EncodeUtf8 = MD5Util.MD5EncodeUtf8(password);
		user.setPassword(md5EncodeUtf8);
		user.setRole(0);

		int insert = mapper.insert(user);
		return insert;
	}

}
